<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\model;


use app\common\model\TimeModel;

/**
 * Class MallGoods
 * @package app\admin\model
 * User : Orange Tel: 181-3414-1353 QQ:1139552211
 * Date    : 2021/6/9 下午5:16
 */
class MallGoods extends TimeModel {

    protected $table = "";

    protected $append = ['type_text'];

    protected $deleteTime = 'delete_time';

    public function cate() {
        return $this->belongsTo('app\admin\model\MallCate', 'cate_id', 'id');
    }

    public function getTypeTextAttr($value, $data) {
        $status = ['1' => '现货', '2' => '期货'];
        return $status[$data['type']];
    }

    /**
     *判断库存是否充足
     * @param $number
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/7 上午10:02
     */
    public function isStockNumber($number) {
        $where   = [];
        $where[] = ['stock', '>', 0];
        $where[] = ['status', '=', 1];
        $stock   = self::instance()->where($where)->find();
        if (empty($stock)) {
            return false;
        }
        if ($stock < $number) {
            return false;
        }

        return true;
    }

    /**
     * 扣除库存
     * @param $all
     * @return mixed
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/9 下午12:11
     */
    public function decProductStock($all) {
        return self::instance()->where([
            ['id', '=', $all['type_id']],
            ['status', '=', 1],
            ['stock', '>=', $all['number']],
        ])->dec('stock', $all['number'])->update();
    }

    /**
     * 获取所有未交割矿机
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/9 下午4:16
     */
    public function getAllNoConsign() {
        return self::instance()->where(['status' => 2])->select()->toArray();
    }

    /**
     * 矿机交割
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/9 下午4:34
     */
    public function consignNode() {
        $no_consign_node = $this->getAllNoConsign();
        foreach ($no_consign_node as $nc_key => $nc_value) {
            if ($nc_value['days_time'] >= time()) {
                $data['status']      = 4;
                $data['update_time'] = time();
                $data['expire_time'] = get_someday_time($nc_value['days_time']);
                self::instance()->where('id', $nc_value['id'])->update($data);
            }
        }
    }

    /**
     * 获取所有运行中矿机
     * @param int $limit
     * @return array|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/9 下午5:16
     */
    public function getAllOutputNode($limit = 5000) {
        //获取所有正常用户总数
        $list = self::instance()->where(['status' => 4])->cache(true)->select()->toArray();
        return $this->filterExpireNode(array_chunk($list, $limit));

    }

    /**
     * 排除到期矿机
     * @param $list
     * @return mixed
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/10 下午12:04
     */
    public function filterExpireNode($list) {
        for ($i = 0; $i < count($list); $i++) {
            $list[$i] = $this->deleteExpireNode($list[$i]);
        }

        return $list;
    }

    /**
     * 修改并去除已到期矿机
     * @param $list
     * @return mixed
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/10 下午12:04
     */
    public function deleteExpireNode($list) {
        foreach ($list as $key => $value) {
            if ($value['expire_time'] >= time()) {
                self::instance()->where(['id' => $value['id']])->update(['status' => 9]);
                unset($list[$key]);
            }
        }

        return $list;
    }


}